# -*- coding: utf-8 -*-
"""
------------------------------------------------------------------------------
    File Name:  samp_logging
    Author   :  wanwei1029
    Date     :  2018/3/5
    Desc     :
------------------------------------------------------------------------------
"""
import sys
import logging


def get_logger(logger_name, file_name=None):
    logger = logging.getLogger(logger_name)
    formatter = logging.Formatter('%(name)s %(asctime)s %(levelname) -8s: %(message)s')
    console_handler = logging.StreamHandler(sys.stdout)
    console_handler.formatter = formatter
    if file_name is not None:
        file_handler = logging.FileHandler(file_name)
        file_handler.formatter = formatter
    logger.addHandler(console_handler)
    if file_name is not None:
        logger.addHandler(file_handler)
    logger.setLevel(logging.INFO)
    # logger.setLevel(logging.DEBUG)
    return logger


if __name__ == '__main__':
    test_method = "get_logger"
    if test_method == "get_logger":
        lg = get_logger('debug', 'test')
        lg.debug("this is debug info.")
        lg2 = get_logger('info','test2')
        lg2.info("this is info msg")
        lg3 = get_logger("info","test3","test3.log")
        lg3.info("this will save to file")